﻿using Atlanchim.Crm.Mui.Commands;
using Atlanchim.Crm.Mui.Helpers;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using CrmEntities = Lmondeil.Crm.EF;

namespace Atlanchim.Crm.Mui.Pages.Contacts
{
    public class PageViewContactModel : AbstractPageViewModel
    {
        /// <summary>
        /// Contact de référence
        /// </summary>
        public Contact Contact { get; set; }

        /// <summary>
        /// Url d'accès à la page d'édition
        /// </summary>
        public override string EditUrl { get; set; }

        /// <summary>
        /// Informations sur les actions
        /// </summary>
        public string ActionsListUrl { get; private set; }

        /// <summary>
        ///  Informations sur les emails
        /// </summary>
        public string EmailsListUrl { get; private set; }

        /// <summary>
        /// Informations sur les téléphones
        /// </summary>
        public string TelephonesListUrl { get; private set; }

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        public PageViewContactModel()
        {
            this.DeleteWarning = "ATTENTION - La suppression du contact entraine la suppression des éléments suivants associés : " + Environment.NewLine +
                "   - Actions" + Environment.NewLine +
                "   - Emails" + Environment.NewLine +
                "   - Téléphones" + Environment.NewLine +
                "   - Alertes";
        }

        /// <summary>
        /// Chargement
        /// </summary>
        public override void Load()
        {
            // Lecture du fragment
            int idContact = 0;
            int.TryParse(this.DicFragments["IdContact"], out idContact);

            // Chargement
            using (CrmContext context = new CrmContext())
            {
                // Contact
                this.Contact = context
                                .Contacts
                                .Where(contact => contact.Id == idContact)
                                .FirstOrDefault();
                this.EditUrl = string.Format("/Pages/Contacts/PageEditContact.xaml#IdContact={0}", idContact);


                // Actions
                this.ActionsListUrl = string.Format("/Pages/Actions/PageListActions.xaml#IdContact={0}", idContact);

                // Emails
                this.EmailsListUrl = string.Format("/Pages/Emails/PageListEmails.xaml#IdContact={0}", idContact);

                // Telephones
                this.TelephonesListUrl = string.Format("/Pages/Telephones/PageListTelephones.xaml#IdContact={0}", idContact);

                // TabLinks
                IEnumerable<Link> bufferLinks = new Link[]
                {
                    new Link(){DisplayName="Actions", Source = new Uri(this.ActionsListUrl, UriKind.RelativeOrAbsolute)},
                    new Link(){DisplayName="Emails", Source = new Uri(this.EmailsListUrl, UriKind.RelativeOrAbsolute)},
                    new Link(){DisplayName="Téléphones", Source = new Uri(this.TelephonesListUrl, UriKind.RelativeOrAbsolute)}
                };
                this.TabLinks = new LinkCollection(bufferLinks);

            }

            // Notification
            OnPropertyChanged("Contact");
        }

        /// <summary>
        /// Suppression
        /// </summary>
        protected override void OnDeleteCurrentEntity()
        {
            CrmCrudHelper.DeleteEntityNewContext(this.Contact);
        }
    }
}
